package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.JazUserCommentClassifyDO;
import com.jinzhi.jzweb.service.JazUserCommentClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazUserCommentDescriptionDao;
import com.jinzhi.jzweb.domain.JazUserCommentDescriptionDO;
import com.jinzhi.jzweb.service.JazUserCommentDescriptionService;
import com.jinzhi.common.base.CoreServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 
 * <pre>
 * 评价描述表
 * </pre>
 * <small> 2024-05-16 15:08:53 | Long</small>
 */
@Service
public class JazUserCommentDescriptionServiceImpl extends CoreServiceImpl<JazUserCommentDescriptionDao, JazUserCommentDescriptionDO> implements JazUserCommentDescriptionService {

    @Autowired
    private JazUserCommentClassifyService jazUserCommentClassifyService;

    @Autowired
    private JazUserCommentDescriptionDao jazUserCommentDescriptionDao;

    /**
     * 分页查询评价描述列表
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<JazUserCommentDescriptionDO> getUserCommentDescriptionList(int pageNumber, int pageSize) {
        Page<JazUserCommentDescriptionDO> page = new Page<>(
                Optional.of(pageNumber).orElse(1),
                Optional.of(pageSize).orElse(10));
        Wrapper<JazUserCommentDescriptionDO> wrapper = new EntityWrapper<JazUserCommentDescriptionDO>().orderBy("create_time", true);
        // 查询列表数据
        Page<JazUserCommentDescriptionDO> selectPage = selectPage(page, wrapper);
        //
        selectPage.getRecords().stream().forEach(jazUserCommentDescriptionDO -> {
            JazUserCommentClassifyDO jazUserCommentClassifyDO = jazUserCommentClassifyService.selectById(jazUserCommentDescriptionDO.getCommentClassifyId());
            jazUserCommentDescriptionDO.setCommentClassifyName(jazUserCommentClassifyDO.getCommentTypeName());
        });
        return selectPage;
    }

    /**
     * 查询评论类型
     *
     * @return
     */
    @Override
    public List<JazUserCommentClassifyDO> getCommentType() {
        Wrapper<JazUserCommentClassifyDO> wrapper = new EntityWrapper<JazUserCommentClassifyDO>().eq("status", 0);
        List<JazUserCommentClassifyDO> list = jazUserCommentClassifyService.selectList(wrapper);
        if (list == null) {
            return null;
        }
        return list;
    }

    /**
     * 新增评价描述
     *
     * @param jazUserCommentDescription
     * @return
     */
    @Override
    public Result<String> saveUserCommentDescription(JazUserCommentDescriptionDO jazUserCommentDescription) {
        if (jazUserCommentDescription == null) {
            return Result.fail();
        }
        boolean insert = insert(jazUserCommentDescription);
        return insert?Result.ok():Result.fail();
    }

    /**
     * 修改评价描述
     *
     * @param jazUserCommentDescription
     * @return
     */
    @Override
    public Result<String> updateUserCommentDescriptionById(JazUserCommentDescriptionDO jazUserCommentDescription) {
        JazUserCommentDescriptionDO jazUserCommentDescriptionDO = selectById(jazUserCommentDescription);
        if (jazUserCommentDescriptionDO == null) {
            return Result.fail();
        }
        jazUserCommentDescriptionDO.setCommentClassifyId(jazUserCommentDescription.getCommentClassifyId());
        jazUserCommentDescriptionDO.setCommentDescription(jazUserCommentDescription.getCommentDescription());
        jazUserCommentDescriptionDO.setLabelStatus(jazUserCommentDescription.getLabelStatus());

        boolean update = updateById(jazUserCommentDescriptionDO);
        return update?Result.ok():Result.fail();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public JazUserCommentDescriptionDO selectUserCommentDescriptionById(Long id) {
        JazUserCommentDescriptionDO jazUserCommentDescriptionDO = selectById(id);
        if (jazUserCommentDescriptionDO == null) {
            return jazUserCommentDescriptionDO;
        }
        // 查询评价类型名称
        Wrapper<JazUserCommentClassifyDO> wrapper = new EntityWrapper<JazUserCommentClassifyDO>().eq("comment_classify_id", jazUserCommentDescriptionDO.getCommentClassifyId());
        JazUserCommentClassifyDO jazUserCommentClassifyDO = jazUserCommentClassifyService.selectOne(wrapper);
        if (jazUserCommentClassifyDO == null){
            return jazUserCommentDescriptionDO;
        }
        // 封装评价类型名称
        jazUserCommentDescriptionDO.setCommentClassifyName(jazUserCommentClassifyDO.getCommentTypeName());
        return jazUserCommentDescriptionDO;
    }

    /**
     * 修改评价描述状态
     *
     * @param jazUserCommentDescriptionDO
     * @return
     */
    @Override
    public Result<String> updateUserCommentDescriptionStatus(JazUserCommentDescriptionDO jazUserCommentDescriptionDO) {
        JazUserCommentDescriptionDO descriptionDO = selectById(jazUserCommentDescriptionDO.getId());
        if (descriptionDO == null){
            return Result.fail();
        }
        descriptionDO.setLabelStatus(jazUserCommentDescriptionDO.getLabelStatus());
        boolean update = updateById(descriptionDO);
        return update?Result.ok():Result.fail();
    }

    /**
     * 评价分类类型查询评论描述
     * @param commentType
     * @return
     */
    @Override
    public Result<?> selCommentDescription(Integer commentType) {
        Map map = new HashMap();
        map.put("commentType",commentType);
        List<JazUserCommentDescriptionDO> jazUserCommentDescriptionDOS = jazUserCommentDescriptionDao.queryDescriptionByType(map);
        return Result.ok(jazUserCommentDescriptionDOS);
    }



}
